import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'mdict_flutter_plugin_platform_interface.dart';

/// An implementation of [MdictFlutterPluginPlatform] that uses method channels.
class MethodChannelMdictFlutterPlugin extends MdictFlutterPluginPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('mdict_flutter_plugin');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  Future<String?> lookforMdict(String path, String word) async {
    final r = await methodChannel.invokeMethod<String>('lookforMdict', jsonEncode({
      "path": path,
      "word": word
    }));
    return r;
  }

  @override
  Future<void> releaseMdict(String path) async {
    await methodChannel.invokeMethod<bool>('releaseMdict', jsonEncode({
      "path": path
    }));
  }

  @override
  Future<int?> getSizeMdict(String path) async {
    final r = await methodChannel.invokeMethod<int>('getSizeMdict', jsonEncode({
      "path": path
    }));
    return r;
  }

  @override
  Future<List<String>?> suggestMdict(String path, String word, String maxCount) async {
    final r = await methodChannel.invokeMethod<List<String>>('lookforMdict', jsonEncode({
      "path": path,
      "word": word,
      "maxCount": maxCount
    }));
    return r;
  }


  @override
  Future<Uint8List?> getResourceMdict(String path, url) async {
    final r = await methodChannel.invokeMethod<Uint8List>('getResourceMdict', jsonEncode({
      "path": path,
      "url": url
    }));
    return r;
  }

  @override
  Future<String?> getNameMdict(String path) async {
    final r = await methodChannel.invokeMethod<String>('getNameMdict', jsonEncode({
      "path": path
    }));
    return r;
  }
}
